package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.constants.AdminConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.util.Date;
import java.util.HashMap;

@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {

    /**
     * 实现登录功能
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(AdUserDto dto) {
        //1.参数校验
        if(dto==null|| StringUtils.isBlank(dto.getName())||StringUtils.isBlank(dto.getPassword())){
            //三者任一为空则返回参数异常
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdUser one = getOne(Wrappers.<AdUser>lambdaQuery()
                .eq(AdUser::getName, dto.getName())
                .eq(AdUser::getStatus, AdminConstants.USER_STATUS_EXIT));
        if (one==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NUMBER_DOTEXIT_OR_CLOSURE);
        }
        //2.数据加密对比
        String pwd=dto.getPassword()+one.getSalt();
        String s = DigestUtils.md5DigestAsHex(pwd.getBytes());
        if(!s.equals(one.getPassword())){
            //两个结果不相等话返回账号或者密码错误
            System.out.println("账号或者密码错误");
            return ResponseResult.errorResult(AppHttpCodeEnum.NUMBER_DOTEXIT_OR_CLOSURE);
        }
        //3.校验成功,刷新这个登录时间
        one.setLoginTime(new Date());
        updateById(one);
        //4.制造token并返回给前端
        String token = AppJwtUtil.getToken(one.getId().longValue());
        HashMap<Object, Object> map = new HashMap<>();
        map.put("token",token);
        one.setSalt(null);
        one.setPassword(null);
        one.setEmail(null);
        one.setPhone(null);
        map.put("user",one);
        //5.返回结果封装回前端
        return ResponseResult.okResult(map);
    }

    /**
     * 实现注册功能
     * @param adUser 新增的用户
     * @return
     */
    @Override
    public ResponseResult register(AdUser adUser) {
        //todo: 实现注册用户的逻辑
        //1.判断前端传递参数是否为空
        if(adUser==null||StringUtils.isBlank(adUser.getName())||
                StringUtils.isBlank(adUser.getPassword())||
                StringUtils.isBlank(adUser.getPhone())||
                StringUtils.isBlank(adUser.getEmail())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.获取用户信息把创建时间置为新设置的值
        adUser.setLoginTime(null);
        adUser.setCreatedTime(new Date());
        adUser.setStatus(AdminConstants.USER_STATUS_EXIT);
        String salt = RandomStringUtils.randomAlphanumeric(6);
        adUser.setSalt(salt);
        String md5Password=adUser.getPassword()+salt;
        String saltPswd = DigestUtils.md5DigestAsHex(md5Password.getBytes());
        //4.拼接新创建的字符串 并将其设为密码
        adUser.setPassword(saltPswd);
        save(adUser);

        //5.返回结果封装
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}